package com.zhangfb95.crosschain.console.service.impl;

import com.zhangfb95.crosschain.console.enums.CommandEnum;
import com.zhangfb95.crosschain.console.command.Command;
import com.zhangfb95.crosschain.console.service.CommandService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangfubing
 * @since 2023/12/21
 */
@Service
public class CommandServiceImpl implements CommandService {

    @Autowired
    private List<Command> commands;
    private final Map<String, Command> commandMap = new HashMap<>();

    @PostConstruct
    public void init() {
        for (Command command : commands) {
            this.commandMap.put(command.command().getCode(), command);
        }
    }

    @Override
    public Command get(String commandName) {
        Command command = commandMap.get(commandName);
        if (command == null) {
            return commandMap.get(CommandEnum.HELP.getCode());
        }
        return command;
    }
}
